package com.test.utils.swing;

import com.test.utils.text.JSONUtils;
import com.test.utils.text.XMLUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

public class SwingUtilPackage extends JPanel {

    private final JTabbedPane tabBox = new JTabbedPane(JTabbedPane.LEFT);// 存放选项卡的组件
    private final String[] tabNames = {"JSON格式化", "XML格式化", "参数拼接"};
    ImageIcon icon = createImageIcon("images/middle.gif");


    public SwingUtilPackage(int x, int y, int width, int height) {
        this.setBounds(0, 30, width, height);
        //this.tabBox.setBounds(x,y,width,height);
        layoutComponents();
    }

    private void createJsonPanel() {
        BasicPanl basicPanl = new BasicPanl();
        JTextPane textPane = ComponentUtils.createTextPane(
                "JSON格式化",
                new Rectangle(10, 10, 890, 400),
                new Color(0, 100, 0),
                basicPanl
        );
        basicPanl.addComponent(textPane);


        ComponentUtils.createScrollPane(
                textPane,
                new Rectangle(2, 20, 890, 440),
                null,
                basicPanl
        );

        ComponentUtils.createButton(
                "===格式化===",
                new Rectangle(6, 475, 150, 30),
                basicPanl,
                BorderFactory.createLineBorder(Color.gray),
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String jsonString = textPane.getText().trim();

                        try {
                            textPane.setText(JSONUtils.jsonFormat(jsonString));
                        } catch (Exception exception) {
                            exception.printStackTrace();
                        }
                    }
                }
        );

        ComponentUtils.createButton(
                "===清除===",
                new Rectangle(180, 475, 150, 30),
                basicPanl,
                BorderFactory.createLineBorder(Color.gray),
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        textPane.setText("");
                    }
                }
        );

        tabBox.addTab(tabNames[0], icon, basicPanl, "Tab1");// 加入第一个页面
        tabBox.setMnemonicAt(0, KeyEvent.VK_0);// 设置第一个位置的快捷键为0
    }

    private void createXmlPanel() {
        BasicPanl basicPanl = new BasicPanl();
        JTextPane xmlPane = ComponentUtils.createTextPane(
                "",
                new Rectangle(10, 10, 920, 400),
                new Color(0, 100, 0),
                basicPanl
        );
        basicPanl.addComponent(xmlPane);


        ComponentUtils.createScrollPane(
                xmlPane,
                new Rectangle(2, 20, 980, 440),
                null,
                basicPanl
        );

        ComponentUtils.createButton(
                "===格式化===",
                new Rectangle(6, 475, 150, 30),
                basicPanl,
                BorderFactory.createLineBorder(Color.gray),
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String xmlString = xmlPane.getText().trim();

                        try {
                            xmlPane.setText(XMLUtils.xmlFormat(xmlString));
                        } catch (Exception exception) {
                            exception.printStackTrace();
                        }
                    }
                }
        );

        ComponentUtils.createButton(
                "===清除===",
                new Rectangle(180, 475, 150, 30),
                basicPanl,
                BorderFactory.createLineBorder(Color.gray),
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        xmlPane.setText("");
                    }
                }
        );

        tabBox.addTab(tabNames[1], icon, basicPanl, "Tab2");// 加入第一个页面
        tabBox.setMnemonicAt(1, KeyEvent.VK_1);// 设置第一个位置的快捷键为0
    }

    private void layoutComponents() {
        int i = 0;
        // json格式化面板
        this.createJsonPanel();
        // XML格式化面板
        this.createXmlPanel();

        // 第3个标签下的JPanel
        JPanel jpanelOutputWsData = new JPanel();
        tabBox.addTab(tabNames[2], icon, jpanelOutputWsData, "Tab3");// 加入第一个页面
        tabBox.setMnemonicAt(1, KeyEvent.VK_2);// 设置快捷键为2
        setLayout(new GridLayout(1, 1));
        add(tabBox);

        // 第4个标签下的JPanel
        /*tabBox.addTab(tabNames[3], icon, new JPanel(), "Tab4");// 加入第一个页面
        tabBox.setMnemonicAt(1, KeyEvent.VK_3);// 设置快捷键为1
        setLayout(new GridLayout(1, 1));
        add(tabBox);*/

        tabBox.setBorder(BorderFactory.createLineBorder(Color.green));

    }


    private ImageIcon createImageIcon(String path) {

        URL url = SwingUtilPackage.class.getResource(path);
        if (url == null) {
            System.out.println("the image " + path + " is not exist!");
            return null;
        }
        return new ImageIcon(url);
    }

    private JPanel createTab1Panel() {
        JPanel panel = new JPanel(false);
        panel.setLayout(null);  // new GridLayout()

        JLabel importDir = new JLabel("Import Folder:");

        JTextField textImportDir = new JTextField(15);

        JButton btnSubmit = new JButton("Submit");

        panel.add(importDir);
        panel.add(textImportDir);
        panel.add(btnSubmit);

        importDir.setBounds(100, 75, 100, 25);
        textImportDir.setBounds(200, 75, 200, 25);
        btnSubmit.setBounds(100, 125, 80, 25);

        return panel;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        BasicPanl box = new BasicPanl();
        box.setLayout(null);


        //tow.setLayout(null);
        Container component = new SwingUtilPackage(30, 0, 1000, 570);
        //component.setLayout(null);
        box.add(component);


        BasicPanl one = new BasicPanl();
        JButton button = ComponentUtils.createButton("设置", new Rectangle(0, 0, 100, 30), one, BorderFactory.createEmptyBorder(0, 0, 0, 0), null);
        //去掉焦点边框
        button.setFocusPainted(false);
        //去掉按钮填充
        button.setContentAreaFilled(false);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton button=(JButton) e.getSource();
                Component component = button.getRootPane().getParent();
                System.out.println(component.getX()+","+component.getY()+","+component.getWidth()+","+component.getHeight());
                Frame newFrame = new JFrame();
                newFrame.setLayout(null);
                //newFrame.setSize(component.getWidth(), component.getHeight());
                //newFrame.setLocation(component.getX()+100,component.getY()+100);
                newFrame.setBounds(component.getX(),component.getY(),component.getWidth(),component.getHeight());
                JLabel label = new JLabel("我是新窗口");
                newFrame.add(label);
                newFrame.setLocationRelativeTo(null);
                newFrame.setVisible(true);
                newFrame.setResizable(false);
                newFrame.setBounds(30, 0, 1000, 570);
                newFrame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println(111);
                        super.windowClosing(e);
                    }
                });

                Toolkit toolkit = Toolkit.getDefaultToolkit();
                int x = (int) (toolkit.getScreenSize().getWidth() - newFrame.getWidth()) / 2;
                int y = (int) (toolkit.getScreenSize().getHeight() - newFrame.getHeight()) / 2;
                newFrame.setLocation(x, y);
                //JOptionPane.showMessageDialog(newFrame, "Cant Minimize");
            }
        });
        one.add(button);
        one.setBounds(0, 0, 1000, 30);
        one.setLayout(null);
        box.add(one);

        JFrame frame = new JFrame("小疯子工具包");
        frame.setContentPane(box);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setSize(1000, 600);
        frame.setVisible(true);
        frame.setLayout(null);

        Toolkit toolkit = Toolkit.getDefaultToolkit();
        int x = (int) (toolkit.getScreenSize().getWidth() - frame.getWidth()) / 2;
        int y = (int) (toolkit.getScreenSize().getHeight() - frame.getHeight()) / 2;
        frame.setLocation(x, y);

        /*SwingUtilities.invokeLater(new Runnable() {
            final Container component = new SwingUtilPackage(new Dimension(990,550));
            public void run() {
                JFrame frame = new JFrame("小疯子工具包");
                frame.setContentPane(component);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.setResizable(false);
                frame.setSize(1000, 600);
                frame.setVisible(true);
                frame.setLayout(null);
            }
        });*/
    }

}
